﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain.Wrappers;
using Cms.Web.Framework.Controls;

namespace Cms.Web.UI.Admin
{
    public partial class DashboardGadgets : AdminPage, IDashboardGadgetsView
    {
        private DashboardGadgetsPresenter _presenter;

        #region IDashboardGadgetsView Members

        public void SetGadgets(IList<DashboardGadgetDescription> gadgets)
        {
            rpGadgets.DataSource = gadgets;
            rpGadgets.DataBind();
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/DashboardGadgets.xml"; }
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new DashboardGadgetsPresenter(this);
            HeadingText = GetText(this, "Heading");
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
            lbDelete.OnClientClick = "return cms.Dialogs.ConfirmLinkButton(this, " +
                                     "function (status) { " +
                                     "if (status == 'ok') " +
                                     "cms.Lists.ShowPaneOverlay('gadgetPaneOverlay');" +
                                     "else " +
                                     "cms.Lists.HidePaneOverlays(); " +
                                     "}, " +
                                     "'" + GetText(this, "DeleteHeader") + "', " +
                                     "'" + GetText(this, "DeleteMessage") + "', " +
                                     "'" + GetText(this, "DeleteOK") + "', " +
                                     "'" + GetText(this, "DeleteCancel") + "', " +
                                     "true);";
        }


        protected void rpGadgets_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var gadgetDescription = (DashboardGadgetDescription) e.Item.DataItem;
            var litID = (Literal) e.Item.FindControl("litID");
            var litName = (Literal) e.Item.FindControl("litName");
            var litDescription = (Literal) e.Item.FindControl("litDescription");
            var litCustom = (Literal) e.Item.FindControl("litCustom");
            litID.Text = "<span id=\"";
            litID.Text += gadgetDescription.Gadget.CustomGadget ? "C" : "G";
            litID.Text += gadgetDescription.Gadget.ID + "\">";
            litName.Text = gadgetDescription.Name;
            litDescription.Text = gadgetDescription.Description;
            litCustom.Text = gadgetDescription.Gadget.CustomGadget ? GetText(this, "Yes") : GetText(this, "No");
        }


        protected void lbDelete_Click(object sender, EventArgs e)
        {
            _presenter.DeleteCustomGadget(int.Parse(hidSelectedGadgetID.Value.Substring(1)));
            upnlGadgets.Update();
        }
    }
}